package com.intelligent.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligent.service.ProductionTasksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/productionTasks")
public class ProductionTasksController {

    @Autowired
    private ProductionTasksService productionTasksService;

    /**
     * 分页联表查询（包含条件查询），直接返回分页结果（Map 列表）
     * 可选查询参数：
     * taskNo, status, workOrderNo, product, processNo,
     * processCode, processName, processCodePc, processNamePn,
     * username,
     * plannedStartDateBegin, plannedStartDateEnd,
     * plannedCompletionDateBegin, plannedCompletionDateEnd
     */
    @GetMapping("/page")
    public IPage<Map<String, Object>> page(@RequestParam(defaultValue = "1") long pageNum,
                                           @RequestParam(defaultValue = "10") long pageSize,
                                           @RequestParam Map<String, Object> allParams) {
        // 移除分页参数，剩余作为查询条件
        Map<String, Object> params = new HashMap<>(allParams);
        params.remove("pageNum");
        params.remove("pageSize");

        Page<Object> page = new Page<>(pageNum, pageSize);
        return productionTasksService.pageWithJoin(page, params);
    }

    /**
     * 按 ID 联表查询（无需VO/DTO），直接返回 Map
     */
    @GetMapping("/{id}")
    public Map<String, Object> getById(@PathVariable("id") Long id) {
        return productionTasksService.getByIdWithJoin(id);
    }

    /**
     * 新增生产任务（任务编号自动生成），直接接收请求体中的字段 Map。
     * 参照字段：status, workOrderId, product, processNoR, processId, processManagementId, uId,
     * plannedStartDate, plannedCompletionDate, numberCompleted, numberCompletedProducts,
     * productionSchedule, numberRework, reworkRate, scrappedQuantity, scrapRate, finalProcess
     * 返回：{"success":true, "affected":1, "taskNo":"..."}
     */
    @PostMapping("/create")
    public Map<String, Object> create(@RequestBody Map<String, Object> body) {
        int affected = productionTasksService.createTask(body);
        Map<String, Object> resp = new HashMap<>();
        resp.put("success", affected > 0);
        resp.put("affected", affected);
        resp.put("taskNo", body.get("taskNo"));
        return resp;
    }

    /**
     * 修改生产任务（根据 id）。
     * 请求体需包含 id 以及需要修改的字段（见 XML 占位符）。
     * 返回：{"success":true, "affected":1}
     */
    @PostMapping("/update")
    public Map<String, Object> update(@RequestBody Map<String, Object> body) {
        int affected = productionTasksService.updateTask(body);
        Map<String, Object> resp = new HashMap<>();
        resp.put("success", affected > 0);
        resp.put("affected", affected);
        return resp;
    }

    /**
     * 删除生产任务（根据 id）。
     * 对应 SQL: DELETE FROM production_tasks WHERE id = #{id}
     * 返回：{"success":true, "affected":1}
     */
    @DeleteMapping("/{id}")
    public Map<String, Object> delete(@PathVariable("id") Long id) {
        int affected = productionTasksService.deleteTaskById(id);
        Map<String, Object> resp = new HashMap<>();
        resp.put("success", affected > 0);
        resp.put("affected", affected);
        return resp;
    }
}
